define(
		[],
		function() {
			webUtils.nameSpace("wiseMen.platform.orgUserSelector");

			/*
			 * 组织机构选择组件
			 * onSelect:function(rows){} 选择回调方法
			 * scope:this,  作用域
			 * isSingle:false,是否单选
			 * hasSelectedOrgs:selectedRows,已选择的单位
			 */
			wiseMen.platform.orgUserSelector.orgSelector = function(params) {
				var orgTree;
				var hasSelectedGrid;
				params = $.extend({
					scope : this,
					isSelectAll:false,
					hasSelectedOrgs : [],
					isSingle : false,
					onSelect : function(rows) {
					}
				}, params);
				var dialog = webUtils.dialog({
							title : '选择组织机构',
							onremove : function() {
								if (params.onClose) {
									params.onClose.call(null);
								}
							},
							buttons : [ {
								label : '关闭',
								action : function(dialog) {
									dialog.close();
								}
							}, {
								label : '确定',
								icon : 'glyphicon glyphicon-check',
								cssClass : 'btn-primary',
								action : function(dialog) {
									var rows = hasSelectedGrid.bootstrapTable("getData");
									params.onSelect.call(params.scope, rows);
									dialog.close();
								}
							} ],
							onshown : function(dialog) {
								var body = dialog.getModalBody();
								body
										.append(wiseMen.platform.orgUserSelector.orgSelector.temp);
								var mainContent = body.find(".mainContent");

								orgTree = new wiseMen.platform.orgUserSelector.orgTree(
										{
											target : mainContent.find("._orgTree"),
											isSelectAll:params.isSelectAll,
											onClick : function(e, treeId,
													treeNode, clickFlag) {
												if (clickFlag != 0) {
													onSelectOrg(treeNode);
												}
											}
										});

								hasSelectedGrid = new wiseMen.platform.orgUserSelector.hasSelectedOrgBox(
										{
											target : mainContent
													.find("._hasSelectedBox"),
											hasSelectedOrgs : params.hasSelectedOrgs
										});
							}
						});

				function onSelectOrg(treeNode) {
					if (treeNode.id == '-1') {
						return;
					}
					var row = hasSelectedGrid.bootstrapTable(
							"getRowByUniqueId", treeNode.id);

					if (!row) {
						if (params.isSingle) {
							hasSelectedGrid.bootstrapTable('removeAll');
						}

						hasSelectedGrid.bootstrapTable("append", treeNode);
					}
				}

			};

			/*
			 * 用户选择组件
			 * onSelect:function(rows){} 选择回调方法
			 * scope:this,  作用域
			 * isSingle:false,是否单选
			 * hasSelectedUsers:selectedRows,已选择的单位
			 */
			wiseMen.platform.orgUserSelector.userSelector = function(params) {
				var orgTree;
				var selectUserBox;
				var hasSelectedGrid;

				params = $.extend({
					scope : this,
					hasSelectedUsers : [],
					isSingle : false,
					onSelect : function(rows) {
					}
				}, params);
				var dialog = webUtils.dialog({
							title : '选择用户',
							size : "w",
							onremove : function() {
								if (params.onClose) {
									params.onClose.call(null);
								}
							},
							buttons : [ {
								label : '关闭',
								action : function(dialog) {
									dialog.close();
								}
							}, {
								label : '确定',
								icon : 'glyphicon glyphicon-check',
								cssClass : 'btn-primary',
								action : function(dialog) {
									var rows = hasSelectedGrid.bootstrapTable("getData");
									params.onSelect.call(params.scope, rows);
									dialog.close();
								}
							} ],
							onshown : function(dialog) {
								var body = dialog.getModalBody();
								body
										.append(wiseMen.platform.orgUserSelector.userSelector.temp);
								var mainContent = body.find(".mainContent");

								orgTree = new wiseMen.platform.orgUserSelector.orgTree(
										{
											target : mainContent
													.find("._orgTree"),
											onClick : function(e, treeId,
													treeNode, clickFlag) {
												if (clickFlag != 0) {
													onSelectOrg(treeNode);
												}
											}
										});

								selectUserBox = new wiseMen.platform.orgUserSelector.selectUserBox(
										{
											target : mainContent
													.find("._selectBox"),
											onSelect : function(row) {
												onSelectUser(row);
											}
										});

								hasSelectedGrid = new wiseMen.platform.orgUserSelector.hasSelectedUserBox(
										{
											target : mainContent
													.find("._hasSelectedBox"),
											hasSelectedUsers : params.hasSelectedUsers
										});
							}
						});

				function onSelectOrg(treeNode) {
					if (treeNode.id == '-1') {
						return;
					}
					selectUserBox.orgId = treeNode.id
					selectUserBox.bootstrapTable("refresh");
				}

				function onSelectUser(row) {
					var rowTarget = hasSelectedGrid.bootstrapTable(
							"getRowByUniqueId", row.id);

					if (!rowTarget) {

						if (params.isSingle) {
							hasSelectedGrid.bootstrapTable('removeAll');
						}

						hasSelectedGrid.bootstrapTable("append", row);
					}
				}

			};

			wiseMen.platform.orgUserSelector.orgTree = function(params) {
				params = $.extend({}, params);
				var orgTree = $.fn.zTree.init(params.target,
						{
							treeId : webUtils.uuid(),
							async : {
								enable : true,
								url : ctx + "/orgAction/queryOrgTreeNodes.do"+(params.isSelectAll?"?startOrg=-1":""),
								autoParam : [ "id=pid" ],
								dataFilter : function(treeId, parentNode,
										responseData) {
									if (responseData) {
										$.each(responseData, function(i, n) {
											if (n.childrenSize > 0) {
												n.isParent = true;
											}
										});
									}
									return responseData;
								}
							},
							data : {
								key : {
									name : "text"
								}
							},
							callback : {
								onClick : params.onClick
							}
						}, [ {
							text : "组织机构",
							id : "-1",
							isParent : true,
							open : true
						} ]);

				orgTree.reAsyncChildNodes(
						orgTree.getNodesByParam("id", "-1")[0], "refresh");
				return orgTree;
			};

			wiseMen.platform.orgUserSelector.hasSelectedOrgBox = function(
					params) {
				params = $.extend({
					hasSelectedOrgs : []
				}, params);
				var grid = params.target.bootstrapTable({
					columns : [ {
						title : '单位名称',
						field : 'name',
						align : 'left',
						width : 300
					} ],
					data : params.hasSelectedOrgs,
					sidePagination : 'client',
					pagination : false,
					uniqueId : "id",
					onClickRow : function(row) {
						grid.bootstrapTable("removeByUniqueId", row.id);
					}
				});

				return grid;
			};

			wiseMen.platform.orgUserSelector.selectUserBox = function(params) {
				params = $.extend({
					onSelect : function(row) {
					}
				}, params);
				var grid = params.target.bootstrapTable({
					url : ctx + "/userAction/queryByOrgId.do?startOrg=-1",
					queryParams : function(d) {
						if (grid) {
							d.orgId = grid.orgId;
						}
						return d;
					},
					columns : [ {
						field : '_s',
						checkbox : true
					}, {
						title : '用户名称',
						field : 'userName',
						align : 'left',
						width : 300
					} ],
					singleSelect:true,
					height : $(window).height() * 0.7,
					onClickRow : params.onSelect
				});

				return grid;
			};

			wiseMen.platform.orgUserSelector.hasSelectedUserBox = function(
					params) {
				params = $.extend({
					hasSelectedUsers : []
				}, params);
				var grid = params.target.bootstrapTable({
					columns : [ {
						title : '用户名称',
						field : 'userName',
						align : 'left',
						width : 300
					} ],
					data : params.hasSelectedUsers,
					sidePagination : 'client',
					pagination : false,
					uniqueId : "id",
					onClickRow : function(row) {
						grid.bootstrapTable("removeByUniqueId", row.id);
					}
				});
				return grid;
			};

			wiseMen.platform.orgUserSelector.orgSelector.temp = '<div class="mainContent row">'
					+ '<div title="单位列表" class="col-md-6"><div class="_orgTree ztree"></div></div>'
					+ '<div title="已选列表" class="col-md-6" ><table class="_hasSelectedBox"></table></div>'
					+ '</div>';

			wiseMen.platform.orgUserSelector.userSelector.temp = '<div class="mainContent row">'
					+ '<div title="单位列表" class="col-md-4"><div class="_orgTree ztree"></div></div>'
					+ '<div title="待选列表" class="col-md-4"><table class="_selectBox"></table></div>'
					+ '<div title="已选列表" class="col-md-4"><table class="_hasSelectedBox"></table></div>'
					+ '</div>';

			return {
				orgSelector : wiseMen.platform.orgUserSelector.orgSelector,
				userSelector : wiseMen.platform.orgUserSelector.userSelector
			}
		});
